*************************************************************************
* 	Replication codes for: 
*	The Distance Between Us: The Role of Ideological Proximity in Shaping 
*	Perceptions of Inter-Partisan Relationships
*		Seonghui Lee
*		Journal of Election, Public Opinion & Parties (accepted, 2024)
*	codes written and run in Stata 16
*************************************************************************

use replicationLee2024Jepop.dta, clear

xtset respid
set scheme plotplain

*---------------------
* Figure 1
*---------------------

hist friends, bin(11) percent normal xlabel(0(1)10) xtitle("Friendship score") title(Pooled) name(yall, replace)
hist friends if cntry=="Canada", bin(11) percent normal  xlabel(0(1)10) xtitle("Friendship score") title(Canada) name(ycnd, replace)
hist friends if cntry=="Germany", bin(11) percent normal  xlabel(0(1)10) xtitle("Friendship score") title(Germany) name(yde, replace)
hist friends if cntry=="UK", bin(11) percent normal  xlabel(0(1)10) xtitle("Friendship score") title(UK)name(yuk, replace)

graph combine yall ycnd yde yuk, ycommon xsize(7) ysize(5) imargin(zero)


*---------------------
* Figure 2
*---------------------

graph hbar (mean) friends if cntry=="Canada", over(dyadid) bar(1, lcolor(%80)) ytitle("") title(Canada) yline(5.14, lcolor(red) lpattern(solid) lwidth(medium)) name(canbarall, replace) legend(off) 

graph hbar (mean) friends if cntry=="Germany", over(dyadid) bar(1, lcolor(%80)) ytitle("") title(Germany) yline(4.69, lcolor(red) lpattern(solid) lwidth(medium)) name(debarall, replace)

graph hbar (mean) friends if cntry=="UK", over(dyadid) bar(1, lcolor(%80)) ytitle("") title(UK) yline(5.15, lcolor(red) lpattern(solid) lwidth(medium)) name(ukbarall, replace)

graph combine canbarall debarall ukbarall, rows(1) xsize(9) ysize(4) b1(Friendship score, size(small)) graphregion(margin(zero)) plotregion(margin(zero)) imargin(zero) scale(1.6) xcommon


*---------------------
* Table A2
*---------------------

tabstat friends dist rile_dist cogovern cogovern_10 cogovern_20 opposition_together vshare_A vshare_B radright_incl special_incl eco_incl male agefull edu polint hpk ispartisan dyadpartisan, c(stat) s(n mean sd min max) f(%9.3f)


*---------------------
* Table A3
*---------------------

mixed friends dist rile_dist cogovern opposition_together vshare_A vshare_B radright_incl special_incl eco_incl male agefull edu polint hpk ispartisan dyadpartisan , ||_all:R.dyad || respid:
	estimates store m1
mixed friends dist rile_dist cogovern_10 opposition_together vshare_A vshare_B radright_incl special_incl eco_incl male agefull edu polint hpk ispartisan dyadpartisan , ||_all:R.dyad || respid:
	estimates store m2
mixed friends dist rile_dist cogovern_10 cogovern_20  opposition_together vshare_A vshare_B radright_incl special_incl eco_incl male agefull edu polint hpk ispartisan dyadpartisan , ||_all:R.dyad || respid:
	estimates store m3
mixed friends dist rile_dist cogovern opposition_together vshare_A vshare_B radright_incl special_incl eco_incl male agefull edu polint hpk ispartisan dyadpartisan i.country, ||_all:R.dyad || respid:
	estimates store m1fe
mixed friends dist rile_dist cogovern_10 opposition_together vshare_A vshare_B radright_incl special_incl eco_incl male agefull edu polint hpk ispartisan dyadpartisan i.country, ||_all:R.dyad || respid:
	estimates store m2fe
mixed friends dist rile_dist cogovern_10 cogovern_20  opposition_together vshare_A vshare_B radright_incl special_incl eco_incl male agefull edu polint hpk ispartisan dyadpartisan i.country, ||_all:R.dyad || respid:
	estimates store m3fe

* table
esttab m1 m2 m3 m1fe m2fe m3fe, star(** 0.05 *** 0.01) label se b(%5.3f) transform(ln*: exp(2*@) 2*exp(2*@)) eqlabels("" "var(dyad)" "var(respondents)" "var(residual)")


*---------------------
* Figure 3
*---------------------

coefplot m1 m1fe||, xline(0, lcolor(black) lpattern(solid)) ci(95) b1title("Coefficients", size(medsmall)) keep(dist rile_dist cogovern opposition_together vshare_A vshare_B radright_incl special_incl eco_incl male agefull edu polint hpk ispartisan dyadpartisan) legend(order(2 "country FE" 4 "no country FE") position(4) ring(0) size(medsmall) rows(2)) headings(dist = "{bf:Dyad characteristics}" male="{bf:Individual characteristics}", labcolor(black)) xlabel(-1.5(.5)2) xsize(6) ysize(5) graphregion(margin(tiny))  plotregion(margin(tiny)) scheme(lean1) scale(.8)


*---------------------
* Table A4
*---------------------

mixed friends dist cogovern  i.country, ||_all:R.dyad || respid:
	estimates store bm1fe
mixed friends dist cogovern opposition_together i.country, ||_all:R.dyad || respid:
	estimates store bm2fe
mixed friends rile_dist cogovern  i.country, ||_all:R.dyad || respid:
	estimates store bm3fe
mixed friends rile_dist cogovern opposition_together i.country, ||_all:R.dyad || respid:
	estimates store bm4fe	
mixed friends dist rile_dist cogovern opposition_together i.country, ||_all:R.dyad || respid:
	estimates store bm5fe

* table
esttab bm1fe bm2fe bm3fe bm4fe bm5fe, keep(dist rile_dist cogovern opposition_together _cons) star(** 0.05 *** 0.01) label se b(%5.3f) transform(ln*: exp(2*@) 2*exp(2*@)) eqlabels("" "var(dyad)" "var(respondents)" "var(residual)")
	

*---------------------
* Table A5
*---------------------

mixed friends rile_dist cogovern opposition_together vshare_A vshare_B radright_incl special_incl eco_incl male agefull edu polint hpk ispartisan dyadpartisan , ||_all:R.dyad || respid:
	estimates store m1B
mixed friends rile_dist cogovern opposition_together vshare_A vshare_B radright_incl special_incl eco_incl male agefull edu polint hpk ispartisan dyadpartisan i.country, ||_all:R.dyad || respid:
	estimates store m1feB
mixed friends ches_dist cogovern opposition_together vshare_A vshare_B radright_incl special_incl eco_incl male agefull edu polint hpk ispartisan dyadpartisan , ||_all:R.dyad || respid:
	estimates store m1C
mixed friends ches_dist cogovern opposition_together vshare_A vshare_B radright_incl special_incl eco_incl male agefull edu polint hpk ispartisan dyadpartisan i.country, ||_all:R.dyad || respid:
	estimates store m1feC
	
esttab m1B m1feB m1C m1feC, star(** 0.05 *** 0.01) label se b(%5.3f) transform(ln*: exp(2*@) 2*exp(2*@)) eqlabels("" "var(dyad)" "var(respondents)" "var(residual)")
	

*---------------------
* Figure A1
*---------------------

* canada

local i = 1
levelsof dyadlabel if cntry=="Canada", local(levels) 
foreach d of local levels {
	sum friends if dyadlabel=="`d'" & cntry=="Canada" 
	local dm : di %3.2f r(mean)
twoway histogram friends if dyadlabel=="`d'" & cntry=="Canada", ///
	discrete percent xtitle("") ytitle("") color(red%30) xline(`dm', lpattern(solid)) ///
	text(28 `dm' "`dm'", color(blue)) title("`d'") graphregion(margin(0 0 0 0)) plotregion(margin(0 0 0 0)) name(p`i', replace) yscale(r(0 30)) nodraw
	local i = `i'+1
}
graph combine p9 p10 p6 p8 p5 p2 p7 p1 p3 p4, l1(% Respondents, size(small)) b1(Friendship Score, size(small)) rows(2) ycommon scale(1.3) xsize(8) ysize(4) title(Canada, size(medsmall)) name(canhist, replace)

* germany

local i = 1
levelsof dyadlabel if cntry=="Germany", local(levels) 
foreach d of local levels {
	sum friends if dyadlabel=="`d'" & cntry=="Germany" 
	local dm : di %3.2f r(mean)
	twoway histogram friends if dyadlabel=="`d'" & cntry=="Germany", ///
	discrete percent xtitle("") ytitle("") color(red%30) xline(`dm', lpattern(solid)) ///
	text(38 `dm' "`dm'", color(blue)) title("`d'") graphregion(margin(0 0 0 0)) plotregion(margin(0 0 0 0)) name(g`i', replace) yscale(r(0 30)) nodraw
	local i = `i'+1
	}
graph combine g14 g4 g7 g11 g15 g5 g13 g9 g10 g6 g3 g8 g12 g1 g2, l1(% Respondents, size(small)) b1(Friendship Score, size(small)) rows(3) ycommon scale(1.2) xsize(8) ysize(5) title(Germany, size(medsmall)) name(dehist, replace)

* uk

local i = 1
levelsof dyadlabel if cntry=="UK", local(levels) 
foreach d of local levels {
	sum friends if dyadlabel=="`d'" & cntry=="UK" 
	local dm : di %3.2f r(mean)
twoway histogram friends if dyadlabel=="`d'" & cntry=="UK", ///
	discrete percent xtitle("") ytitle("") color(red%30) xline(`dm', lpattern(solid)) ///
	text(28 `dm' "`dm'", color(blue)) title("`d'") graphregion(margin(0 0 0 0)) plotregion(margin(0 0 0 0)) name(u`i', replace) yscale(r(0 30)) nodraw
	local i = `i'+1
}
graph combine u10 u6 u8 u7 u2 u3 u5 u13 u11 u1 u4 u14 u12 u9  u15, l1(% Respondents, size(small)) b1(Friendship Score, size(small)) rows(3) ycommon scale(1.2) xsize(8) ysize(5) title(UK, size(medsmall)) name(ukhist, replace) 



*\END
